package com.asset.dispoal.business.bsAgreement.service.commom;

import com.asset.dispoal.business.bsAgreement.domain.db.entity.AgreementCaseState;
import com.asset.dispoal.business.bsAgreement.domain.db.entity.AgreementStateRecord;
import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.business.bsCase.domain.db.entity.CaseStateRecord;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;

@Component
public class AgreementCloseCaseCommand implements BaseCommand<String, Result> {

    @Autowired
    private AgreementMapper agreementMapper;

    @Autowired
    private BsCaseMapper caseMapper;

    @Autowired
    private BaseIdeable<String> ideable;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result execute(String id) {
        List<AgreementCaseState> agreementCaseStateList = agreementMapper.queryAgreementCaseStateByAgreementId(id);
        boolean closeChecked = true;
        String oldState = null, oldCaseState = null;
        String caseId = agreementCaseStateList.get(0).getCaseId();
        for (AgreementCaseState agreementCaseState : agreementCaseStateList) {
            if (id.equals(agreementCaseState.getAgreementId())) {
                oldState = agreementCaseState.getCaseState();
                oldCaseState = agreementCaseState.getState();
                continue;
            }
            if (!AgreementCaseSateMenu.CLOSE_CASE.getLabel().equals(agreementCaseState.getCaseState())) {
                closeChecked = false;
            }
        }
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        if (closeChecked) {
            //结案更新状态
            caseMapper.updateCaseState(caseId, AgreementCaseSateMenu.CLOSE_CASE.getLabel(), "1", userId);
            CaseStateRecord caseStateRecord = new CaseStateRecord();
            caseStateRecord.setId(ideable.generateId());
            caseStateRecord.setCaseId(caseId);
            caseStateRecord.setCreateUser(userId);
            caseStateRecord.setCreateTime(DateTimeUtil.currentDate());
            caseStateRecord.setOldState(oldCaseState);
            caseStateRecord.setNewState(AgreementCaseSateMenu.CLOSE_CASE.getLabel());
            caseMapper.insertStateRecord(caseStateRecord);
        }

        agreementMapper.updateCaseState(id, AgreementCaseSateMenu.CLOSE_CASE.getLabel(), "1", userId);
        AgreementStateRecord agreementStateRecord = new AgreementStateRecord();
        agreementStateRecord.setId(ideable.generateId());
        agreementStateRecord.setContractId(id);
        agreementStateRecord.setCreateUser(userId);
        agreementStateRecord.setCreateTime(DateTimeUtil.currentDate());
        agreementStateRecord.setOldState(oldState);
        agreementStateRecord.setNewState(AgreementCaseSateMenu.CLOSE_CASE.getLabel());
        agreementMapper.insertStateRecord(agreementStateRecord);
        return Result.ok();
    }
}
